package com.water.freemarker;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.watermud.util.EasyMap;

import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.Version;

public class FreeMarkerConfigWraper {
	final static Logger log = LoggerFactory.getLogger(FreeMarkerConfigWraper.class);

	private static FreeMarkerConfigWraper instance = new FreeMarkerConfigWraper();

	private Configuration configuration;

	public static FreeMarkerConfigWraper getInstance() {
		return instance;
	}

	private FreeMarkerConfigWraper() {
		configuration = new Configuration();
	}

	public void load(String dir) throws IOException {
		// Specify the data source where the template files come from. Here I
		// set a
		// plain directory for it, but non-file-system are possible too:
		configuration.setDirectoryForTemplateLoading(new File(dir));

		// Specify how templates will see the data-model. This is an advanced
		// topic...
		// for now just use this:
		configuration.setObjectWrapper(new DefaultObjectWrapper());

		// Set your preferred charset template files are stored in. UTF-8 is
		// a good choice in most applications:
		configuration.setDefaultEncoding("UTF-8");

		// Sets how errors will appear. Here we assume we are developing HTML
		// pages.
		// For production systems TemplateExceptionHandler.RETHROW_HANDLER is
		// better.
		configuration.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER);

		// At least in new projects, specify that you want the fixes that aren't
		// 100% backward compatible too (these are very low-risk changes as far
		// as the
		// 1st and 2nd version number remains):
		configuration.setIncompatibleImprovements(new Version(2, 3, 20)); // FreeMarker
		// 2.3.20
	}

	public Template getTemplate(String name) throws IOException {
		return configuration.getTemplate(name);
	}

	public static void main(String[] args) {
		String freemarker = "freemarker";
		log.info("设置FreeMarker文件目录为：" + freemarker);
		FreeMarkerConfigWraper config = FreeMarkerConfigWraper.getInstance();
		try {
			config.load(freemarker);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		String beanName = "PeopleLocation";
		String packageName = "com.water.freemarker";
		List list = new ArrayList();
		list.add(EasyMap.create().put("javaType", "long").put("name", "id").map());
		list.add(EasyMap.create().put("javaType", "String").put("name", "name").map());
		Map data = EasyMap.create().put("package", packageName).put("className", beanName).put("listx", list).put("author", "lihua.sun").map();
		try {
			FreeMarkerUtil.print(System.out, "bean.ftl", data);
		} catch (IOException | TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
